package www.hz.domain.strategy.service.raffle;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import www.hz.domain.strategy.model.entity.RaffleFactorEntity;
import www.hz.domain.strategy.model.entity.RuleActionEntity;
import www.hz.domain.strategy.model.entity.RuleMatterEntity;
import www.hz.domain.strategy.model.entity.StrategyAwardEntity;
import www.hz.domain.strategy.model.valobj.RuleTreeVO;
import www.hz.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import www.hz.domain.strategy.model.vo.StrategyAwardRuleModelVO;
import www.hz.domain.strategy.repository.IStrategyRepository;
import www.hz.domain.strategy.service.AbstractRaffleStrategy;
import www.hz.domain.strategy.service.IRaffleAward;
import www.hz.domain.strategy.service.IRaffleStock;
import www.hz.domain.strategy.service.armory.IStrategyDispatch;
import www.hz.domain.strategy.service.rule.chain.ILogicChain;
import www.hz.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import www.hz.domain.strategy.service.rule.filter.ILogicFilter;
import www.hz.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import www.hz.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import www.hz.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import www.hz.types.common.Constants;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: DefaultRaffleStrategy
 * @Description: TODO
 * @version: v1.８.0
 * @author: hz
 * @date: 2025/3/13 15:52
 */
@Slf4j
@Service
public class DefaultRaffleStrategy extends AbstractRaffleStrategy  implements IRaffleStock, IRaffleAward {
    public DefaultRaffleStrategy(IStrategyRepository repository, IStrategyDispatch strategyDispatch, DefaultChainFactory defaultChainFactory, DefaultTreeFactory defaultTreeFactory) {
      super(repository, strategyDispatch, defaultChainFactory, defaultTreeFactory);
    }
    @Override
    public DefaultChainFactory.StrategyAwardVO raffleLogicChain(String userId, Long strategyId) {
        ILogicChain logicChain = defaultChainFactory.openLogicChain(strategyId);
        return  logicChain.logic(userId, strategyId);
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO raffleLogicTree(String userId, Long strategyId, Integer awardId) {
        return null;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO raffleLogicTree(String userId, Long strategyId, Integer awardId, Date endDateTime) {
        StrategyAwardRuleModelVO strategyAwardRuleModelVO = repository.queryStrategyAwardRuleModelVO(strategyId, awardId);
        if (StringUtils.isBlank(strategyAwardRuleModelVO.getRuleModels())) return DefaultTreeFactory.StrategyAwardVO.builder().awardId(awardId).build();
        RuleTreeVO ruleTreeVO = repository.queryRuleTreeVOByTreeId(strategyAwardRuleModelVO.getRuleModels());
        IDecisionTreeEngine decisionTreeEngine = defaultTreeFactory.openLogicTree(ruleTreeVO);
        DefaultTreeFactory.StrategyAwardVO strategyAwardVO = decisionTreeEngine.process(userId, strategyId, awardId);
        return strategyAwardVO;
    }

    @Override
    public List<StrategyAwardEntity> queryRaffleStrategyAwardList(Long strategyId) {
        return repository.queryStrategyAwardList(strategyId);
    }
}